﻿<link rel="import" href="../components/polymer/polymer.html">
<link rel="import" href="post-service.html">
<link rel="import" href="post-card.html">

<polymer-element name="post-list" attributes="show">
  <template>
    <style>
    :host {
      display: block;
      width: 100%;
    }
    post-card {
      margin-bottom: 30px;
    }
    </style>
    
   <post-service id="service" posts="{{posts}}"></post-service> 
   <div layout vertical center>
      <template repeat="{{post in posts}}">
        <post-card favorite="{{post.favorite}}"
      on-favorite-tap="{{handleFavorite}}"
      hidden?="{{show == 'favorites' && !post.favorite}}">
          <img src="{{post.avatar}}" width="70" height="70">
          <h2>{{post.username}}</h2>
          <p>{{post.text}}</p>
        </post-card>
      </template>
    </div>
  </template>

  <script>
  Polymer({
    handleFavorite: function(event, detail, sender) {
      var post = sender.templateInstance.model.post;
      this.$.service.setFavorite(post.uid, post.favorite);
    },
    attached:function(){
      //alert(0);
      this.$.service.setOneFavorite(0);
      this.$.service.setOneFavorite(4);
      this.$.service.setOneFavorite(6);
    }
  });
  </script>

</polymer-element>
